// services/api.js
// API服务层

const appStore = require('../store/index')

/**
 * API基础配置
 */
const API_CONFIG = {
  baseUrl: 'https://api.foreverlove.com',
  timeout: 10000,
  version: 'v1'
}

/**
 * HTTP请求方法
 * @typedef {'GET'|'POST'|'PUT'|'DELETE'} HttpMethod
 */

/**
 * 请求配置
 */
/**
 * 请求配置接口
 * @typedef {Object} RequestConfig
 * @property {string} url
 * @property {'GET'|'POST'|'PUT'|'DELETE'} method
 * @property {any} data
 * @property {number} timeout
 * @property {boolean} needAuth
 */

/**
 * 响应数据格式
 */
/**
 * API响应接口
 * @typedef {Object} ApiResponse
 * @property {number} code
 * @property {string} message
 * @property {any} data
 * @property {boolean} success
 */

/**
 * 用户信息接口
 * @typedef {Object} UserInfo
 * @property {string} id
 * @property {string} nickname
 * @property {string} avatar
 * @property {number} gender
 * @property {string} city
 * @property {string} province
 * @property {string} country
 * @property {string} token
 */

/**
 * 记忆胶囊接口
 * @typedef {Object} MemoryCapsule
 * @property {string} id
 * @property {string} title
 * @property {string} content
 * @property {string} emotion
 * @property {string[]} tags
 * @property {string[]} images
 * @property {string} audio
 * @property {string} location
 * @property {string} weather
 * @property {boolean} isPrivate
 * @property {string} scheduledTime
 * @property {string} createdAt
 * @property {string} updatedAt
 * @property {string} userId
 * @property {UserInfo} userInfo
 */

/**
 * 记忆条目接口
 * @typedef {Object} MemoryEntry
 * @property {string} id
 * @property {string} capsuleId
 * @property {string} content
 * @property {string} emotion
 * @property {string[]} images
 * @property {string} audio
 * @property {string} createdAt
 * @property {string} userId
 */

/**
 * 分页参数接口
 * @typedef {Object} PageParams
 * @property {number} page
 * @property {number} size
 * @property {string} keyword
 * @property {string} emotion
 * @property {string[]} tags
 * @property {string} startTime
 * @property {string} endTime
 */

/**
 * 分页响应接口
 * @typedef {Object} PageResponse
 * @property {Array} list
 * @property {number} total
 * @property {number} page
 * @property {number} size
 * @property {boolean} hasMore
 */

/**
 * API服务类
 */
class ApiService {
  baseURL = ''
  
  constructor() {
    this.init()
  }
  
  init() {
    this.baseURL = this.getBaseURL()
  }
  
  getBaseURL() {
    return API_CONFIG.baseUrl
  }
  
  /**
   * 通用请求方法
   */
  request(options) {
    const { url, method = 'GET', data, header = {} } = options
    
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${this.baseURL}${url}`,
        method: method,
        data,
        header,
        success: (res) => {
          resolve(res.data)
        },
        fail: reject
      })
    })
  }
  
  /**
   * GET请求
   */
  async login(data) {
    return this.request({ url: '/auth/login', method: 'POST', data })
  }
  
  async getUserInfo() {
    return this.request({ url: '/user/info' })
  }
  
  async updateUserInfo(data) {
    return this.request({ url: '/user/info', method: 'PUT', data })
  }
  
  async logout() {
    return this.request({ url: '/auth/logout', method: 'POST' })
  }
  
  async getCapsules(params) {
    return this.request({ url: '/capsules', data: params })
  }
  
  async getCapsule(id) {
    return this.request({ url: `/capsules/${id}` })
  }
  
  async createCapsule(data) {
    return this.request({ url: '/capsules', method: 'POST', data })
  }
  
  async updateCapsule(id, data) {
    return this.request({ url: `/capsules/${id}`, method: 'PUT', data })
  }
  
  async deleteCapsule(id) {
    return this.request({ url: `/capsules/${id}`, method: 'DELETE' })
  }
  
  async getEntries(capsuleId, params) {
    return this.request({ url: `/capsules/${capsuleId}/entries`, data: params })
  }
  
  async createEntry(data) {
    return this.request({ url: '/entries', method: 'POST', data })
  }
  
  async updateEntry(id, data) {
    return this.request({ url: `/entries/${id}`, method: 'PUT', data })
  }
  
  async deleteEntry(id) {
    return this.request({ url: `/entries/${id}`, method: 'DELETE' })
  }
  
  async uploadFile(filePath, type = 'image') {
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: `${this.baseURL}/upload`,
        filePath,
        name: 'file',
        formData: { type },
        success: (res) => {
          resolve(JSON.parse(res.data))
        },
        fail: reject
      })
    })
  }
  
  async getWeather(location) {
    return this.request({ url: '/weather', data: { location } })
  }
  
  async searchCapsules(keyword, params) {
    return this.request({ url: '/search/capsules', data: { keyword, ...params } })
  }
  
  async getEmotionStats() {
    return this.request({ url: '/stats/emotion' })
  }
  
  async getSystemInfo() {
    return this.request({ url: '/system/info' })
  }
  

}

module.exports = new ApiService()
module.exports.ApiService = ApiService